package com.moyu.music;

import com.moyu.activity.PanelGame;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.*;

public class MusicPlayer {
    private String path;
    Player player;
    File file;
    PanelGame panelGame;

    public MusicPlayer(String s, PanelGame panelGame) {
        path = s;
        file = new File(path);
        this.panelGame = panelGame;
    }

    //单次播放
    public void Play() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileInputStream fis = null;
                BufferedInputStream stream = null;
                try {
                    fis = new FileInputStream(file);
                    stream = new BufferedInputStream(fis);
                    player = new Player(stream);
                    player.play();
                } catch (FileNotFoundException | JavaLayerException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        stream.close();
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    //循环播放
    public void loopPlay() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    FileInputStream fis = null;
                    BufferedInputStream stream = null;
                    try {
                        fis = new FileInputStream(file);
                        stream = new BufferedInputStream(fis);
                        player = new Player(stream);
                        player.play();
                    } catch (FileNotFoundException | JavaLayerException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            stream.close();
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}